﻿<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./site.css" rel="stylesheet">
<title>compress/zlib</title>
</head>
<body>
<div class="container">
    <h2 id="pkg-overview">package zlib</h2>
    <p><code>import "compress/zlib"</code>
    <p align="left">zlib包实现了对zlib格式压缩数据的读写，参见<a href="http://tools.ietf.org/html/rfc1950">RFC 1950</a>。</p>
    <p align="left">本包的实现提供了在读取时解压和写入时压缩的滤镜。例如，将压缩数据写入一个bytes.Buffer：</p>
    <pre>var b bytes.Buffer
w := zlib.NewWriter(&amp;b)
w.Write([]byte(&#34;hello, world\n&#34;))
w.Close()
</pre>
    <p>然后将数据读取回来：</p>
    <pre>r, err := zlib.NewReader(&amp;b)
io.Copy(os.Stdout, r)
r.Close()</pre>
    <h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">&para;</a></h3>
    <ul class="list-unstyled">
        <li><a href="#pkg-constants">Constants</a></li>
        <li><a href="#pkg-variables">Variables</a></li>
        <li><a href="#NewReader">func NewReader(r io.Reader) (io.ReadCloser, error)</a></li>
        <li><a href="#NewReaderDict">func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error)</a></li>
        <li><a href="#Writer">type Writer</a></li>
        <ul>
            <li><a href="#NewWriter">func NewWriter(w io.Writer) *Writer</a></li>
            <li><a href="#NewWriterLevel">func NewWriterLevel(w io.Writer, level int) (*Writer, error)</a></li>
            <li><a href="#NewWriterLevelDict">func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)</a></li>
            <li><a href="#Writer.Close">func (z *Writer) Close() error</a></li>
            <li><a href="#Writer.Flush">func (z *Writer) Flush() error</a></li>
            <li><a href="#Writer.Reset">func (z *Writer) Reset(w io.Writer)</a></li>
            <li><a href="#Writer.Write">func (z *Writer) Write(p []byte) (n int, err error)</a></li>
        </ul>
    </ul>
    <h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-examples">&para;</a></h4>
    <ul class="list-unstyled">
        <li><a href="#example-NewReader" onclick="$('#ex-NewReader').addClass('in').removeClass('collapse').height('auto')">NewReader</a></li>
        <li><a href="#example-NewWriter" onclick="$('#ex-NewWriter').addClass('in').removeClass('collapse').height('auto')">NewWriter</a></li>
    </ul>
    <h3 id="pkg-constants">Constants <a class="permalink" href="#pkg-constants">&para;</a></h3>
    <pre>const (
    <span id="NoCompression">NoCompression</span>      = <a href="https://godoc.org/compress/flate">flate</a>.<a href="https://godoc.org/compress/flate#NoCompression">NoCompression</a>
    <span id="BestSpeed">BestSpeed</span>          = <a href="https://godoc.org/compress/flate">flate</a>.<a href="https://godoc.org/compress/flate#BestSpeed">BestSpeed</a>
    <span id="BestCompression">BestCompression</span>    = <a href="https://godoc.org/compress/flate">flate</a>.<a href="https://godoc.org/compress/flate#BestCompression">BestCompression</a>
    <span id="DefaultCompression">DefaultCompression</span> = <a href="https://godoc.org/compress/flate">flate</a>.<a href="https://godoc.org/compress/flate#DefaultCompression">DefaultCompression</a>
)</pre>
    <p>这些常量都是拷贝自flate包，因此导入"compress/zlib"后，就不必再导入"compress/flate"了。</p>
    <h3 id="pkg-variables">Variables <a class="permalink" href="#pkg-variables">&para;</a></h3>
    <pre>var (
    <span class="com">// 当读取的zlib数据的校验和错误时，会返回ErrChecksum</span>
    <span id="ErrChecksum">ErrChecksum</span> = <a href="https://godoc.org/errors">errors</a>.<a href="https://godoc.org/errors#New">New</a>(&#34;zlib: invalid checksum&#34;)
    <span class="com">// 当读取的zlib数据的目录错误时，会返回ErrDictionary</span>
    <span id="ErrDictionary">ErrDictionary</span> = <a href="https://godoc.org/errors">errors</a>.<a href="https://godoc.org/errors#New">New</a>(&#34;zlib: invalid dictionary&#34;)
    <span class="com">// 当读取的zlib数据的头域错误时，会返回ErrHeader</span>
    <span id="ErrHeader">ErrHeader</span> = <a href="https://godoc.org/errors">errors</a>.<a href="https://godoc.org/errors#New">New</a>(&#34;zlib: invalid header&#34;)
)</pre>
    <h3 id="NewReader">func <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/reader.go?name=release#58">NewReader</a> <a class="permalink" href="#NewReader">&para;</a></h3>
    <pre class="funcdecl">func NewReader(r <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Reader">Reader</a>) (<a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#ReadCloser">ReadCloser</a>, <a href="https://godoc.org/builtin#error">error</a>)</pre>
    <p>NewReader返回一个从r读取并解压数据的io.ReadCloser。其实现会缓冲输入流的数据，并可能从r中读取比需要的更多的数据。调用者有责任在读取完毕后调用返回值的Close方法。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-NewReader">
            <div class="panel-heading" onclick="document.getElementById('ex-NewReader').style.display = document.getElementById('ex-NewReader').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-NewReader" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,
    47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
b := bytes.NewReader(buff)
r, err := zlib.NewReader(b)
if err != nil {
    panic(err)
}
io.Copy(os.Stdout, r)</pre>
                    <p>Output:
                    <pre>hello, world
</pre>
                </div>
            </div>
        </div>
    </div>
    <h3 id="NewReaderDict">func <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/reader.go?name=release#64">NewReaderDict</a> <a class="permalink" href="#NewReaderDict">&para;</a></h3>
    <pre class="funcdecl">func NewReaderDict(r <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Reader">Reader</a>, dict []<a href="https://godoc.org/builtin#byte">byte</a>) (<a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#ReadCloser">ReadCloser</a>, <a href="https://godoc.org/builtin#error">error</a>)</pre>
    <p align="left">NewReaderDict类似NewReader，但会使用预设的字典初始化返回的Reader。</p>
    <p align="left">如果压缩数据没有采用字典，本函数会忽略该参数。</p>
    <h3 id="Writer">type <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#26">Writer</a> <a class="permalink" href="#Writer">&para;</a></h3>
    <pre>type Writer struct {
    <span class="com">// 内含隐藏或非导出字段</span>
}</pre>
    <p>Writer将提供给它的数据压缩后写入下层io.Writer接口。</p>
    <h4 id="NewWriter">func <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#42">NewWriter</a> <a class="permalink" href="#NewWriter">&para;</a></h4>
    <pre class="funcdecl">func NewWriter(w <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Writer">Writer</a>) *<a href="#Writer">Writer</a></pre>
    <p align="left">NewWriter创建并返回一个Writer。写入返回值的数据都会在压缩后写入w。</p>
    <p align="left">调用者有责任在结束写入后调用返回值的Close方法。因为写入的数据可能保存在缓冲中没有刷新入下层。</p>
    <div class="panel-group">
        <div class="panel panel-default" id="example-NewWriter">
            <div class="panel-heading" onclick="document.getElementById('ex-NewWriter').style.display = document.getElementById('ex-NewWriter').style.display=='none'?'block':'none';">Example</div>
            <div id="ex-NewWriter" class="panel-collapse collapse">
                <div class="panel-body">
                    <pre>var b bytes.Buffer
w := zlib.NewWriter(&amp;b)
w.Write([]byte(&#34;hello, world\n&#34;))
w.Close()
fmt.Println(b.Bytes())</pre>
                    <p>Output:
                    <pre>[120 156 202 72 205 201 201 215 81 40 207 47 202 73 225 2 4 0 0 255 255 33 231 4 147]
</pre>
                </div>
            </div>
        </div>
    </div>
    <h4 id="NewWriterLevel">func <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#53">NewWriterLevel</a> <a class="permalink" href="#NewWriterLevel">&para;</a></h4>
    <pre class="funcdecl">func NewWriterLevel(w <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Writer">Writer</a>, level <a href="https://godoc.org/builtin#int">int</a>) (*<a href="#Writer">Writer</a>, <a href="https://godoc.org/builtin#error">error</a>)</pre>
    <p align="left">NewWriterLevel类似NewWriter但指定了压缩水平而不是采用默认的DefaultCompression。</p>
    <p align="left">参数level可以是DefaultCompression、NoCompression或BestSpeed与BestCompression之间包括二者的任何整数。如果level合法，返回的错误值为nil。</p>
    <h4 id="NewWriterLevelDict">func <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#62">NewWriterLevelDict</a> <a class="permalink" href="#NewWriterLevelDict">&para;</a></h4>
    <pre class="funcdecl">func NewWriterLevelDict(w <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Writer">Writer</a>, level <a href="https://godoc.org/builtin#int">int</a>, dict []<a href="https://godoc.org/builtin#byte">byte</a>) (*<a href="#Writer">Writer</a>, <a href="https://godoc.org/builtin#error">error</a>)</pre>
    <p>NewWriterLevelDict类似NewWriterLevel但还指定了用于压缩的字典。dict参数可以为nil；否则，在返回的Writer关闭之前，其内容不可被修改。</p>
    <h4 id="Writer.Reset">func (*Writer) <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#76">Reset</a> <a class="permalink" href="#Writer.Reset">&para;</a></h4>
    <pre class="funcdecl">func (z *<a href="#Writer">Writer</a>) Reset(w <a href="https://godoc.org/io">io</a>.<a href="https://godoc.org/io#Writer">Writer</a>)</pre>
    <p>Reset将w重置，丢弃当前的写入状态，并将下层输出目标设为dst。效果上等价于将w设为使用dst和w的压缩水平、字典重新调用NewWriterLevel或NewWriterLevelDict返回的*Writer。</p>
    <h4 id="Writer.Write">func (*Writer) <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#146">Write</a> <a class="permalink" href="#Writer.Write">&para;</a></h4>
    <pre class="funcdecl">func (z *<a href="#Writer">Writer</a>) Write(p []<a href="https://godoc.org/builtin#byte">byte</a>) (n <a href="https://godoc.org/builtin#int">int</a>, err <a href="https://godoc.org/builtin#error">error</a>)</pre>
    <p>Write将p压缩后写入下层io.Writer接口。压缩后的数据不一定会立刻刷新，除非Writer被关闭或者显式的刷新。</p>
    <h4 id="Writer.Flush">func (*Writer) <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#166">Flush</a> <a class="permalink" href="#Writer.Flush">&para;</a></h4>
    <pre class="funcdecl">func (z *<a href="#Writer">Writer</a>) Flush() <a href="https://godoc.org/builtin#error">error</a></pre>
    <p>Flush将缓冲中的压缩数据刷新到下层io.Writer接口中。</p>
    <h4 id="Writer.Close">func (*Writer) <a title="View Source" href="http://code.google.com/p/go/source/browse/src/pkg/compress/zlib/writer.go?name=release#179">Close</a> <a class="permalink" href="#Writer.Close">&para;</a></h4>
    <pre class="funcdecl">func (z *<a href="#Writer">Writer</a>) Close() <a href="https://godoc.org/builtin#error">error</a></pre>
    <p>调用Close会刷新缓冲并关闭w，但不会关闭下层io.Writer接口。</p>
</div>
</body>
</html>
